/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId myTask02Handle;
osThreadId myTask03Handle;
osThreadId myTask04Handle;
osThreadId _UpdateStepTaskHandle;
osMessageQId UDP_MsgQueueHandle;
osSemaphoreId SPI3_CpltSemHandle;
osSemaphoreId SPI1_CpltSemHandle;
osSemaphoreId RxDn_IdleSemHandle;
osSemaphoreId TxDn_CpltSemHandle;
osSemaphoreId RxUp_IdleSemHandle;
osSemaphoreId TxUp_CpltSemHandle;
osSemaphoreId ToDnSendSemHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);
void UDP_CommTask(void const * argument);
void RxFromDn_Task(void const * argument);
void RxFromUp_Task(void const * argument);
void UpdateStepTask(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* Create the semaphores(s) */
  /* definition and creation of SPI3_CpltSem */
  osSemaphoreDef(SPI3_CpltSem);
  SPI3_CpltSemHandle = osSemaphoreCreate(osSemaphore(SPI3_CpltSem), 1);

  /* definition and creation of SPI1_CpltSem */
  osSemaphoreDef(SPI1_CpltSem);
  SPI1_CpltSemHandle = osSemaphoreCreate(osSemaphore(SPI1_CpltSem), 1);

  /* definition and creation of RxDn_IdleSem */
  osSemaphoreDef(RxDn_IdleSem);
  RxDn_IdleSemHandle = osSemaphoreCreate(osSemaphore(RxDn_IdleSem), 1);

  /* definition and creation of TxDn_CpltSem */
  osSemaphoreDef(TxDn_CpltSem);
  TxDn_CpltSemHandle = osSemaphoreCreate(osSemaphore(TxDn_CpltSem), 1);

  /* definition and creation of RxUp_IdleSem */
  osSemaphoreDef(RxUp_IdleSem);
  RxUp_IdleSemHandle = osSemaphoreCreate(osSemaphore(RxUp_IdleSem), 1);

  /* definition and creation of TxUp_CpltSem */
  osSemaphoreDef(TxUp_CpltSem);
  TxUp_CpltSemHandle = osSemaphoreCreate(osSemaphore(TxUp_CpltSem), 1);

  /* definition and creation of ToDnSendSem */
  osSemaphoreDef(ToDnSendSem);
  ToDnSendSemHandle = osSemaphoreCreate(osSemaphore(ToDnSendSem), 1);

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* definition and creation of UDP_MsgQueue */
  osMessageQDef(UDP_MsgQueue, 4, uint32_t);
  UDP_MsgQueueHandle = osMessageCreate(osMessageQ(UDP_MsgQueue), NULL);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of myTask02 */
  osThreadDef(myTask02, UDP_CommTask, osPriorityHigh, 0, 128);
  myTask02Handle = osThreadCreate(osThread(myTask02), NULL);

  /* definition and creation of myTask03 */
  osThreadDef(myTask03, RxFromDn_Task, osPriorityBelowNormal, 0, 128);
  myTask03Handle = osThreadCreate(osThread(myTask03), NULL);

  /* definition and creation of myTask04 */
  osThreadDef(myTask04, RxFromUp_Task, osPriorityLow, 0, 128);
  myTask04Handle = osThreadCreate(osThread(myTask04), NULL);

  /* definition and creation of _UpdateStepTask */
  osThreadDef(_UpdateStepTask, UpdateStepTask, osPriorityIdle, 0, 128);
  _UpdateStepTaskHandle = osThreadCreate(osThread(_UpdateStepTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
__weak void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_UDP_CommTask */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_UDP_CommTask */
__weak void UDP_CommTask(void const * argument)
{
  /* USER CODE BEGIN UDP_CommTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END UDP_CommTask */
}

/* USER CODE BEGIN Header_RxFromDn_Task */
/**
* @brief Function implementing the myTask03 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_RxFromDn_Task */
__weak void RxFromDn_Task(void const * argument)
{
  /* USER CODE BEGIN RxFromDn_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END RxFromDn_Task */
}

/* USER CODE BEGIN Header_RxFromUp_Task */
/**
* @brief Function implementing the myTask04 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_RxFromUp_Task */
__weak void RxFromUp_Task(void const * argument)
{
  /* USER CODE BEGIN RxFromUp_Task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END RxFromUp_Task */
}

/* USER CODE BEGIN Header_UpdateStepTask */
/**
* @brief Function implementing the _UpdateStepTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_UpdateStepTask */
__weak void UpdateStepTask(void const * argument)
{
  /* USER CODE BEGIN UpdateStepTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END UpdateStepTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

